import {http} from '../../utils/http'
Page( {  
  data: {  
    imgs: [],
    imgssub: [],
    count: 3,
    tabArr: {  
      curHdIndex: 0,  
      curBdIndex: 0  
    },  
  },  
  tabFun: function(e){  
    //获取触发事件组件的dataset属性  
    var _datasetId=e.target.dataset.id;  
    var _obj={};  
    _obj.curHdIndex=_datasetId;  
    _obj.curBdIndex=_datasetId;  
    this.setData({  
      tabArr: _obj  
    });  
  },  
  onLoad: function( options ) {  
    console.info(options)
  },
  bindUpload: function (e) {
    switch (this.data.imgs.length) {
      case 0:
        this.data.count = 4
        break
      case 1:
        this.data.count = 4
        break
      case 2:
        this.data.count = 4
        break
    }
    var that = this
    wx.chooseImage({
      count: that.data.count, // 默认3
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (resource) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = resource.tempFilePaths
        for (let i = 0; i < tempFilePaths.length; i++) {
          wx.showLoading({title: '加载中…'})
          wx.uploadFile({
            url: wx.baseUri + '/upload_sx',
            filePath: tempFilePaths[i],
            name: "file",
            formData: {
              sid:  wx.getStorageSync('sid')||'12345'
            },
            header: {
              "content-type": "multipart/form-data"
            },
            success: function (res) {
              wx.hideLoading()
              let servername = JSON.parse(res.data).data
              if (res.statusCode == 200) {
                wx.getFileSystemManager().readFile({
                  filePath: tempFilePaths[i], //选择图片返回的相对路径
                  encoding: 'base64', //编码格式
                  success: resource => { //成功的回调
                    console.log('data:image/png;base64,' + resource.data)  //输出base64
                    that.data.imgs.push('data:image/png;base64,' + resource.data)
                    that.data.imgssub.push(servername)
                    that.setData({
                      imgs: that.data.imgs
                    })
                  }
                })
                wx.showToast({
                  title: "上传成功",
                  icon: "none",
                  duration: 1500
                })
              }
            },
            fail: function (err) {
              wx.showToast({
                title: "上传失败",
                icon: "none",
                duration: 2000
              })
            },
            complete: function (result) {
              console.log(result.errMsg)
            }
          })
        }
      }
    })
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否删除",
      success: function (res) {
        if (res.confirm) {

          for (let i = 0; i < that.data.imgs.length; i++) {
            if (i == e.currentTarget.dataset.index) {
              let filename = that.data.imgssub[i]
              http.request({
                url: '/upload_del',
                data: {filename},
                success(data) {
                  wx.showToast({
                    title: `删除成功！`,
                    icon: 'success',
                    duration:1000,
                  })

                  that.data.imgs.splice(i, 1)
                  that.data.imgssub.splice(i, 1)
                  that.setData({
                    imgs: that.data.imgs
                  })
                  that.setData({
                    imgssub: that.data.imgssub
                  })
                }
              })
            }
          }
        } else if (res.cancel) {
          console.log("用户点击取消")
        }
      }
    })
  },

  previewImg:function(e){
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.imgs;
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
});



// wx.showModal({
//   title: ' ',
//   content: postStor ? '收藏该文章?' : '取消收藏该文章？',
//   showCancel: 'true',
//   cancelText: '取消',
//   cancelColor: '#333',
//   confirmText: '确认',
//   confirmColor: '#405f80',
//   success: function (res) {
//     if (res.confirm) {
//       wx.setStorageSync('icessun', postsStorage);
//       _this.setData({
//         collected: postStor
//       })
//     }
//   }
// })